using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Common.Utilities;

namespace Workflows.Components.Reports
{
    [Serializable]
    public class Record : IComparable
    {
        private string _IDFieldName;
        private List<Tuple> _Fields;

        public string IDFieldName
        {
            get { return _IDFieldName; }
            set { _IDFieldName = value; }
        }

        public List<Tuple> Fields
        {
            get { return _Fields; }
            set { _Fields = value; }
        }

        public string KeyValue
        {
            get
            {
                string keyVal = string.Empty;
                for (int i = 0; i < this._Fields.Count; i++)
                {
                    if (this._Fields[i].Name.ToLower() == this._IDFieldName.ToLower())
                    {
                        keyVal = this._Fields[i].Value;
                        break;
                    }
                }
                return keyVal;
            }
        }

        public static AggregateDataType DistributionDataType(System.Data.SqlDbType sqlDbType)
        {
            AggregateDataType dataType = AggregateDataType.Unknown;
            switch (sqlDbType)
            {
                case System.Data.SqlDbType.BigInt:
                case System.Data.SqlDbType.Decimal:
                case System.Data.SqlDbType.Float:
                case System.Data.SqlDbType.Int:
                case System.Data.SqlDbType.Money:
                case System.Data.SqlDbType.Real:
                case System.Data.SqlDbType.SmallInt:
                case System.Data.SqlDbType.SmallMoney:
                case System.Data.SqlDbType.TinyInt:
                    dataType = AggregateDataType.Number;
                    break;
                case SqlDbType.Char:
                case SqlDbType.VarChar:
                case SqlDbType.NChar:
                case SqlDbType.NVarChar:
                    dataType = AggregateDataType.String;
                    break;
                case SqlDbType.NText:
                case SqlDbType.Text:
                    dataType = AggregateDataType.Clob;
                    break;
                case SqlDbType.Bit:
                    dataType = AggregateDataType.Bit;
                    break;
                case SqlDbType.DateTime:
                case SqlDbType.SmallDateTime:
                    dataType = AggregateDataType.Time;
                    break;
                case SqlDbType.Binary:
                case SqlDbType.Image:
                case SqlDbType.VarBinary:
                    dataType = AggregateDataType.Blob;
                    break;
                default:
                    break;
            }
            return dataType;
        }

        public Tuple this[string fieldName]
        {
            get
            {
                Tuple tuple = null;
                for (int i = 0; i < this._Fields.Count; i++)
                {
                    if (this._Fields[i].Name.ToLower() == fieldName.ToLower())
                    {
                        tuple = this._Fields[i];
                        break;
                    }
                }
                return tuple;
            }
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            if (obj.GetType() == typeof(Record))
            {
                Record otherRecord = (Record)obj;
                return this.KeyValue.ToLower().CompareTo(otherRecord.KeyValue.ToLower());
            }
            else
                return 0;
        }

        #endregion
    }
}
